import axios from 'axios';
import router from '@/router'

/**
 * api接口基本路径
 */
export const API_BASE_URL = 'http://' 

// http request 拦截器
axios.interceptors.request.use(
    config => {
        return config
    }, function (error) {
        return Promise.reject(error)
    }
)

// http response 拦截器
axios.interceptors.response.use(function (response) {
    return response
}, function (error) {
    return Promise.reject(error)
})

const axiosCreate = (type, path, data) => {
    if (type === 'get') {
        return instance({
            method: type,
            url: API_BASE_URL + path,
            params: data
        })
    } else if (type === 'post') {
        return instance({
            method: type,
            url: API_BASE_URL + path,
            data: data,
            changeOrigin:true
        })
    }
}

const http = (type, path, data) => {
    return new Promise((resolve, reject) => {
        axiosCreate(type, path, data).then(res => {
            resolve(res)
        }).catch(res => {
            Msg.error('发送的响应无效')
            reject(res)
        })
    })
}


// 获取分类
export const getCategory = (data) => http('post', '/getCategory', data)

// 获取列表
export const getList = (data) => http('post', '/getList', data)

// 获取详情
export const getDetail = (data) => http('post', '/getDetail', data)

